package com.xgpro.mapper.message;

import com.xgpro.domain.messagemanage.entity.Message;
import org.apache.ibatis.annotations.*;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @program: xgpro__dev
 * @description: dao层接口
 * @author: likai
 * @create: 2019-09-17 07:09
 **/

public interface MessageMapper extends Mapper<Message> {

    @Select("<script> " +
            "select t1.id as id,t1.title as title,t1.content as content,t1.send_user as sendUser,t1.msg_time as msgTime,t1.type as type " +
            "from t_message t1 join t_user_msg t2 on t1.id = t2.msg_id and t2.user_id =#{userId} and t2.is_read = #{isRead} order by t1.msg_time desc" +
            " <if test=\"limit != null and limit!='' \">" +
            " limit 1 " +
            " </if> " +
            "</script>")
    List<Message> queryList(@Param("userId") Integer userId, @Param("isRead") String isRead, @Param("limit") String limit);

    @Insert("<script> " +
            "insert into t_user_msg(user_id,msg_id,create_time) values(#{userId},#{msgId},(select now()))" +
            "</script>")
    int sendInform(@Param("userId") int userId, @Param("msgId") int msgId);

    @Select("<script> " +
            "select count(1) from t_user_msg where user_id=#{userId} and is_read=#{isRead}" +
            "</script>")
    int getCount(@Param("userId") Integer userId,@Param("isRead") String isRead);

    /**
     * 修改消息为已读
     *
     * @param msgId
     * @param useId
     * @return
     */
    @Update("<script> " +
            "update t_user_msg set is_read = 1 where 1=1 " +
            "<if test=\"msgId != null\">" +
            " and msg_id=#{msgId}" +
            " </if>" +
            " and user_id=#{userId}" +
            "</script>")
    int updateActive(@Param("msgId") Integer msgId, @Param("userId") Integer useId);

    @Delete("<script> " +
            "DELETE FROM t_message WHERE msg_time <![CDATA[< ]]> #{format} "+
            "</script>")
    int deleteByTime(String format);

    @Delete("<script> " +
            "DELETE FROM t_user_msg WHERE user_id = #{userId} "+
            "</script>")
    int deleteMsgByUserId(Integer userId);
}
